package tree.reader;

import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

import tree.person.Person;
import tree.utils.ImageReader;

public class StaXParser {
	static final String PERSON = "tree.person.Person";
	static final String ID = "id";
	static final String MUM_ID = "mumId";
	static final String DAD_ID = "dadId";
	static final String NAME = "name";
	static final String SURNAME = "surname";
	static final String SEX = "sex";
	static final String FOTO = "foto";


	@SuppressWarnings({ "unchecked", "null" })
	public List<Person> readConfig(String configFile, boolean lazy) {
		List<Person> persons = new ArrayList<Person>();
		try {
			// First create a new XMLInputFactory
			XMLInputFactory inputFactory = XMLInputFactory.newInstance();
			// Setup a new eventReader
			InputStream in = new FileInputStream(configFile);
			XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
			// Read the XML document
			Person person = null;

			while (eventReader.hasNext()) {
				XMLEvent event = eventReader.nextEvent();

				if (event.isStartElement()) {
					StartElement startElement = event.asStartElement();
					// If we have a Person element we create a new Person
					if (startElement.getName().getLocalPart() == (PERSON)) {
						person = new Person();
					}

					if (event.isStartElement()) {
						if (event.asStartElement().getName().getLocalPart().equals(ID)) {
							event = eventReader.nextEvent();
							person.setId(Long.parseLong(event.asCharacters().getData()));
							continue;
						}
					}
					if (event.isStartElement()) {
						if (event.asStartElement().getName().getLocalPart().equals(MUM_ID)) {
							event = eventReader.nextEvent();
							person.setMumId(Long.parseLong(event.asCharacters().getData()));
							continue;
						}
					}
					if (event.asStartElement().getName().getLocalPart().equals(DAD_ID)) {
						event = eventReader.nextEvent();
						person.setDadId(Long.parseLong(event.asCharacters().getData()));
						continue;
					}
					
					if (!lazy) {
						if (event.asStartElement().getName().getLocalPart().equals(NAME)) {
							event = eventReader.nextEvent();
							person.setName(event.asCharacters().getData());
							continue;
						}

						if (event.asStartElement().getName().getLocalPart().equals(SURNAME)) {
							event = eventReader.nextEvent();
							person.setSurname(event.asCharacters().getData());
							continue;
						}

						if (event.asStartElement().getName().getLocalPart().equals(SEX)) {
							event = eventReader.nextEvent();
							person.setSex(event.asCharacters().getData());
							continue;
						}

						if (event.asStartElement().getName().getLocalPart().equals(FOTO)) {
							event = eventReader.nextEvent();
							String foto = event.asCharacters().getData();
							person.setFoto(foto);
							BufferedImage img = ImageReader.readImage(foto);
							person.setImage(img);
							continue;
						}
					}
				}
				// If we reach the end of an Person element we add it to the
				// list
				if (event.isEndElement()) {
					EndElement endElement = event.asEndElement();
					if (endElement.getName().getLocalPart() == (PERSON)) {
						persons.add(person);
						
					}
				}

			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (XMLStreamException e) {
			e.printStackTrace();
		}
		return persons;
	}

}
